package com.example.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pojo.MsslyyAddressMessaget;
import com.example.pojo.MsslyyBook;
import com.example.pojo.MsslyyOrderDetail;
import com.example.pojo.MsslyyOrderMaster;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.MsslyyOrderMasterMapper;
import com.example.utils.DateUtil;
import com.example.utils.OrderNumberUtils;
import com.example.utils.RedisUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MsslyyOrderMasterService extends ServiceImpl<MsslyyOrderMasterMapper, MsslyyOrderMaster> {

    @Resource
    private MsslyyOrderMasterMapper msslyyOrderMasterMapper;
    @Resource
    private OrderNumberUtils orderNumberUtils;
    @Resource
    private MsslyyOrderDetailService msslyyOrderDetailService;
    @Resource
    private MsslyyBookService msslyyBookService;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private DateUtil dateUtil;


    /**
     * 添加订单主表记录
     * @param userId 用户ID
     * @param address 下单地址
     * @param orderMasterMoney 总金额
     * @return
     */
    public MsslyyOrderMaster addOrder(Long userId, MsslyyAddressMessaget address, BigDecimal orderMasterMoney){
            MsslyyOrderMaster msslyyOrderMaster=new MsslyyOrderMaster();
            //生成订单编号
            String uuid = orderNumberUtils.getUUID();
            msslyyOrderMaster.setOrderMasterNumber(uuid);
            msslyyOrderMaster.setUserId(userId);
            msslyyOrderMaster.setAddressMessageName(address.getAddressMessageName());
            msslyyOrderMaster.setAddressMessagePhone(address.getAddressMessagePhone());
            msslyyOrderMaster.setAddressMessageProvince(address.getAddressMessageProvince());
            msslyyOrderMaster.setAddressMessageCity(address.getAddressMessageCity());
            msslyyOrderMaster.setAddressMessageDistrict(address.getAddressMessageDistrict());
            msslyyOrderMaster.setAddressMessageAddress(address.getAddressMessageAddress());
            msslyyOrderMaster.setOrderMasterMoney(orderMasterMoney);
            //保存订单主表记录
            boolean save = save(msslyyOrderMaster);
            LambdaQueryWrapper<MsslyyOrderMaster> query = new LambdaQueryWrapper<MsslyyOrderMaster>().eq(MsslyyOrderMaster::getOrderMasterNumber, uuid);
            MsslyyOrderMaster one = getOne(query);
            //设置凌晨失效
            long expireTime = (dateUtil.getEveryDayTime() - System.currentTimeMillis())/1000;
             //添加到今日营业额
             redisUtil.lSet("turnover",orderMasterMoney,expireTime);

        return one;
    }


    public List<MsslyyOrderMaster> historyOrder(Long userId){
        List<MsslyyBook> books = msslyyBookService.list();
        LambdaQueryWrapper<MsslyyOrderMaster> queryWrapper = new QueryWrapper<MsslyyOrderMaster>()
                .lambda()
                .eq(MsslyyOrderMaster::getUserId, userId)
                .orderByDesc(MsslyyOrderMaster::getId);
        //拿到当前用户订单主表
        List<MsslyyOrderMaster> order = list(queryWrapper);
        //把订单详情放入对应主表
        order.forEach(e->{
            LambdaQueryWrapper<MsslyyOrderDetail> queryWrapper2 = new QueryWrapper<MsslyyOrderDetail>()
                .lambda()
                .eq(MsslyyOrderDetail::getOrderMasterId, e.getId());
            List<MsslyyOrderDetail> orderDetail = msslyyOrderDetailService.list(queryWrapper2);
            orderDetail.forEach(o->{
                books.forEach(b->{
                    if (o.getBookId().equals(b.getId())){
                        o.setBookImg(b.getBookImages());
                    }
                });
            });
            e.setOrderDetails(orderDetail);
        });
        return order;
    }

    /**
     * 确认收货
     * @param orderId
     * @return
     */
    public boolean confirmReceipt(Long orderId){
        MsslyyOrderMaster orderMaster = getById(orderId);
        orderMaster.setOrderMasterReceiveTime(new Date());
        boolean update = updateById(orderMaster);
        return update;



    }
}
